var searchMod_image = {
  name:"image",
  help:"image [keyword] - search images in daum",
  init : function()
  { },
  run: function(args, callback)
  {
    this.callback = callback;

    var query = args[0];
    var category = 'image';

	if (!query)
    {
		query='none datas';
	}

    if (query)
    {
      searchMod_image.search = document.createElement('script');
      searchMod_image.search.type ='text/javascript';
      searchMod_image.search.charset ='utf-8';
      searchMod_image.search.src = 'http://apis.daum.net/search/'+category+'?apikey=457826efc72b71ca78527f24e9d2cfee33b493f1&output=json&callback=searchMod_image.pongSearch&q=' + encodeURI(query);
      document.getElementsByTagName('head')[0].appendChild(searchMod_image.search);
    }
  },
  pongSearch : function(z)
  {
    var output = Daush.Interpreter.createOutput();
    console.debug(output);

    output.innerHTML = '';
    for (var i = 0; i < Math.min(10, z.channel.item.length); i++)
    {
      var li = document.createElement('li');
      var a = document.createElement('a');
      var p = document.createElement('p');
      a.href = z.channel.item[i].link;
      a.innerHTML = searchMod_image.escapeHtml(z.channel.item[i].title);
      p.innerHTML =  searchMod_image.escapeHtml('<a href="'+z.channel.item[i].link+'" target="_blank"><img src='+z.channel.item[i].thumbnail+' /></a>');
       
      li.appendChild(a);
      li.appendChild(p);
      output.appendChild(li);
    }
    this.callback( output );
  },
  escapeHtml : function(str) 
  {
    str = str.replace(/&amp;/g, "&");
    str = str.replace(/&lt;/g, "<");
    str = str.replace(/&gt;/g, ">");
    return str;
  }
};

searchMod_image.init();
Daush.Interpreter.add_module( searchMod_image );



var searchMod_video = {
  name:"video",
  help:"video [keyword] - search vidoes in daum",
  init : function()
  { },
  run: function(args, callback)
  {
    this.callback = callback;
    var query = args[0];
    var category = 'vclip';

	if (!query)
    {
		query='none datas';
	}

    if (query)
    {
      searchMod_video.search = document.createElement('script');
      searchMod_video.search.type ='text/javascript';
      searchMod_video.search.charset ='utf-8';
      searchMod_video.search.src = 'http://apis.daum.net/search/'+category+'?apikey=457826efc72b71ca78527f24e9d2cfee33b493f1&output=json&callback=searchMod_video.pongSearch&q=' + encodeURI(query);
      document.getElementsByTagName('head')[0].appendChild(searchMod_video.search);
    }
  },
  pongSearch : function(z)
  {
    var output = Daush.Interpreter.createOutput();
    console.debug(output);

    output.innerHTML = '';
    for (var i = 0; i < Math.min(10, z.channel.item.length); i++)
    {
      var li = document.createElement('li');
      var a = document.createElement('a');
      var p = document.createElement('p');
      a.href = z.channel.item[i].link;
      a.innerHTML = searchMod_video.escapeHtml(z.channel.item[i].title);
      p.innerHTML =  searchMod_image.escapeHtml('<a href="'+z.channel.item[i].link+'" target="_blank"><img src='+z.channel.item[i].thumbnail+' /></a>');
       
      li.appendChild(a);
      li.appendChild(p);
      output.appendChild(li);
    }
    this.callback( output );
  },
  escapeHtml : function(str) 
  {
    str = str.replace(/&amp;/g, "&");
    str = str.replace(/&lt;/g, "<");
    str = str.replace(/&gt;/g, ">");
    return str;
  }
};

searchMod_video.init();
Daush.Interpreter.add_module( searchMod_video );


var searchMod_news = {
  name:"news",
  help:"news [keyword] - search news in daum",
  init : function()
  { },
  run: function(args, callback)
  {
    this.callback = callback;
    var query = args[0];
    var category = 'news';

	if (!query)
    {
		query='none datas';
	}

    if (query)
    {
      searchMod_news.search = document.createElement('script');
      searchMod_news.search.type ='text/javascript';
      searchMod_news.search.charset ='utf-8';
      searchMod_news.search.src = 'http://apis.daum.net/search/'+category+'?apikey=457826efc72b71ca78527f24e9d2cfee33b493f1&output=json&callback=searchMod_news.pongSearch&q=' + encodeURI(query);
      document.getElementsByTagName('head')[0].appendChild(searchMod_news.search);
    }
  },
  pongSearch : function(z)
  {
    var output = Daush.Interpreter.createOutput();
    console.debug(output);

    output.innerHTML = '';
    for (var i = 0; i < Math.min(10, z.channel.item.length); i++)
    {
      var li = document.createElement('li');
      var a = document.createElement('a');
      var p = document.createElement('p');
      a.href = z.channel.item[i].link;
      a.innerHTML = searchMod_news.escapeHtml(z.channel.item[i].title);
      p.innerHTML =  searchMod_image.escapeHtml(z.channel.item[i].description);
       
      li.appendChild(a);
      li.appendChild(p);
      output.appendChild(li);
    }
    this.callback( output );
  },
  escapeHtml : function(str) 
  {
    str = str.replace(/&amp;/g, "&");
    str = str.replace(/&lt;/g, "<");
    str = str.replace(/&gt;/g, ">");
    return str;
  }
};

searchMod_news.init();
Daush.Interpreter.add_module( searchMod_news );



var searchMod_book = {
  name:"book",
  help:"book [keyword] - search books in daum",
  init : function()
  { },
  run: function(args, callback)
  {
    this.callback = callback;
    var query = args[0];
    var category = 'book';

	if (!query)
    {
		query='none datas';
	}

    if (query)
    {
      searchMod_book.search = document.createElement('script');
      searchMod_book.search.type ='text/javascript';
      searchMod_book.search.charset ='utf-8';
      searchMod_book.search.src = 'http://apis.daum.net/search/'+category+'?apikey=457826efc72b71ca78527f24e9d2cfee33b493f1&output=json&callback=searchMod_book.pongSearch&q=' + encodeURI(query);
      document.getElementsByTagName('head')[0].appendChild(searchMod_book.search);
    }
  },
  pongSearch : function(z)
  {
    var output = Daush.Interpreter.createOutput();
    console.debug(output);

    output.innerHTML = '';
    try
    {
    	for (var i = 0; i < Math.min(10, z.channel.item.length); i++)
	{
      	   var li = document.createElement('li');
	   var a = document.createElement('a');
      	   var p = document.createElement('p');
           a.href = z.channel.item[i].link;
           a.innerHTML = searchMod_book.escapeHtml(z.channel.item[i].title);
           p.innerHTML =  searchMod_image.escapeHtml('<a href="'+z.channel.item[i].link+'" target="_blank"><img src='+z.channel.item[i].cover_s_url+' /></a>');
       
           li.appendChild(a);
           li.appendChild(p);
           output.appendChild(li);
	}
    }
    catch (err) {}

    this.callback( output );
  },
  escapeHtml : function(str) 
  {
    str = str.replace(/&amp;/g, "&");
    str = str.replace(/&lt;/g, "<");
    str = str.replace(/&gt;/g, ">");
    return str;
  }
};

searchMod_book.init();
Daush.Interpreter.add_module( searchMod_book );
